package com.hz.web.controller;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.hz.web.entity.QtUserEntity;
import com.hz.web.entity.XyServiceEntity;
import com.hz.web.service.IxyService;
import com.hz.web.util.ParmsEntity;
import com.hz.web.util.UtilMethods;

/**
 * 信用服务控制层
 * 
 * @author Administrator
 */
@Controller
@RequestMapping("xyService")
public class XyServiceController{

    @Resource
    private IxyService xyService;
    
    /**
     * 删除文件
     */
    @RequestMapping("delFile")
    @ResponseBody
    public void delFile(HttpServletRequest request,String fileurlStr,String fileurl,String sysId ){
    	String path = request.getSession().getServletContext().getRealPath("");
		path = path + File.separator + "down";
		String filePath = path + File.separator + fileurl;
    	UtilMethods.delFile(filePath);
    	if(sysId!=null && !"".equals(sysId.trim())){
    		Map<String, Object> parmsMap = new HashMap<String, Object>();
    		parmsMap.put("fileurlStr", fileurlStr);
    		parmsMap.put("sysId", sysId);
    		xyService.updateAttachment(parmsMap);
    	}
    }

    /**
     * 查询信用服务
     * 
     * @param sysId
     * @return
     */
    @RequestMapping("queryXyService")
    @ResponseBody
    public JSONObject queryXyService(String sysId) {
        JSONObject resultJson = new JSONObject();
        Map<String, Object> parmsMap = new HashMap<String, Object>();
        parmsMap.put("sysId", sysId);
        List<XyServiceEntity> entityLs = xyService.queryXyService(parmsMap);
        resultJson.put("list", entityLs);
        return resultJson;
    }

    /**
     * 添加信用服务
     * 
     * @param entity
     * @return
     */
    @RequestMapping("addXyService")
    @ResponseBody
    public JSONObject addXyService(XyServiceEntity entity, HttpSession session) {
        JSONObject resultJson = new JSONObject();
        if (session.getAttribute("qtuserinfo") == null) {
            resultJson.put("mess", "未登录");
            return resultJson;
        }
        QtUserEntity u = (QtUserEntity) session.getAttribute("qtuserinfo");
        entity.setOperator(u.getUsername());
        int row = xyService.addXyService(entity);
        resultJson.put("row", row);
        return resultJson;
    }

    /**
     * 根据信用服务类型查询文本
     * 
     * @param entity
     * @return
     * @throws UnsupportedEncodingException
     */
    @RequestMapping("queryXyHtml")
    @ResponseBody
    public Map<String, Object> queryXyHtml(ParmsEntity entity, String sort, String inputVal) throws UnsupportedEncodingException {
        if (inputVal != null && !"".equals(inputVal.trim())) {
            // inputVal = URLDecoder.decode(URLDecoder.decode(inputVal));
            inputVal = URLDecoder.decode(URLDecoder.decode(inputVal, "utf-8"), "utf-8");
        }
        Map<String, Object> result = new HashMap<String, Object>();// 结果返回map
        Map<String, Object> parmsMap = new HashMap<String, Object>();// 参数map
        int type = entity.getCkType();
        int page = entity.getPage();
        int rows = entity.getRows();
        if (inputVal != null && !"".equals(inputVal.trim())) {
            parmsMap.put("inputVal", "%" + inputVal.trim() + "%");
        }
        parmsMap.put("type", type);
        parmsMap.put("sort", sort);
        if (page != 0 && rows != 0) {
            int index = (page - 1) * rows;
            int end = rows;
            parmsMap.put("index", index);
            parmsMap.put("end", end);
            int count = 0;
            count = xyService.queryXyHtmlCount(parmsMap);
            result.put("total", count);
        }

        // 根据参数map查询出的结果集合
        List<XyServiceEntity> list = new ArrayList<XyServiceEntity>();
        list = xyService.queryXyService(parmsMap);

        String htmlName = "";
        if (type == 1) {
            htmlName = "信用监督";
        } else if (type == 2) {
            htmlName = "信用咨询";
        } else if (type == 3) {
            htmlName = "信用申报";
        } else if (type == 4) {
            htmlName = "信用异议";
        }

        result.put("htmlName", htmlName);
        result.put("rows", list);

        return result;
    }

}
